/**
 * 
 * If you have edited the file, please update
 * all the last-modified fields manually. Thanks!
 *
 * @last-modified by: 
 * @last-modified date: 
 * @last-modified time: 
 *
 * @author Swee Zhi
 * @date-created Sep 26, 2011    @time-created 11:50:43 PM
 *
 * @project: 
 * @module: 
 * @institution: 
 *
 */
package util.generator;

import entity.UserAccessRightsEntity;
import java.util.List;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import util.entityhelper.StaffAccountState;
import util.enumeration.AccessRight;
import util.localization.MenuLocalization;

public class StaffMenuGenerator {

    private StaffAccountState staffAccountState;
    private MenuModel model;
    private MenuLocalization menuLocalization;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private transient FacesContext context;
    private transient UIViewRoot view;
//    private Menu menu;

    public StaffMenuGenerator(StaffAccountState staffAccountState) {
        this.staffAccountState = staffAccountState;
        model = new DefaultMenuModel();
//        menu = new Menu();
//                menu.setTiered(true);
//        menu.setModel(model);


        menuLocalization = new MenuLocalization();

    }

    private String getRequestURL() {
        Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if (request instanceof HttpServletRequest) {
            return ((HttpServletRequest) request).getRequestURL().toString();
        } else {
            return "";
        }
    }

    private void initContextMemebers() {
        context = FacesContext.getCurrentInstance();
        view = context.getViewRoot();
    }

    public MenuModel generateStaffMenu() {
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
        //initContextMemebers();
        initContextMemebers();
        List<UserAccessRightsEntity> staffAccessList = staffAccountState.getAccessRights();

        generateMailBoxMenu();

        MenuItem menu1 = new MenuItem();
        menu1.setValue(menuLocalization.getStaffMenuItem("MANAGE_ACCOUNT_DETAILS"));
        String url = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffViewAccount.xhtml";
        menu1.setUrl(url);
        menu1.setId(view.createUniqueId());
        model.addMenuItem(menu1);

        MenuItem menu9 = new MenuItem();
        menu9.setValue("View All Stakeholders");
        String url2 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/TCubeViewAllStakeholder.xhtml";
        menu9.setUrl(url2);
        menu9.setId(view.createUniqueId());
        model.addMenuItem(menu9);
     

        MenuItem menu11 = new MenuItem();
        menu11.setValue("Redemption List");
        menu11.setId(view.createUniqueId());
        MenuItem menu12 = new MenuItem();
        menu12.setValue("Add Redemption List");
        menu12.setId(view.createUniqueId());
        String url7 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/TCubeAddRedemptionList.xhtml";
        menu12.setUrl(url7);
        menu11.getChildren().add(menu12);
        MenuItem menu13 = new MenuItem();
        menu13.setValue("View All");
        menu13.setId(view.createUniqueId());
        String url8 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/TCubeViewAllRedemptionList.xhtml";
        menu13.setUrl(url8);
        menu11.getChildren().add(menu13);
        model.addMenuItem(menu11);

        MenuItem menu2 = new MenuItem();
        menu2.setValue(menuLocalization.getStaffMenuItem("VERIFY_STAKEHOLDER_ACCOUNT"));
        String url9 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffVerifyStakeholderAcct.xhtml";
        menu2.setUrl(url9);
        menu2.setId(view.createUniqueId());
        model.addMenuItem(menu2);

        System.out.println("size of staffaccess list:  " + staffAccessList.size());
        for (int i = 0; i < staffAccessList.size(); i++) {
            if (staffAccessList.get(i).getAccessRight().toString().equalsIgnoreCase(AccessRight.ADD_TCUBE_USER.toString())) {
                MenuItem menu3 = new MenuItem();
                menu3.setValue(menuLocalization.getStaffMenuItem(AccessRight.ADD_TCUBE_USER.toString()));
                menu3.setId(view.createUniqueId());
                String url10 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffRegister.xhtml";
                menu3.setUrl(url10);
                model.addMenuItem(menu3);
            } else if (staffAccessList.get(i).getAccessRight().toString().equalsIgnoreCase(AccessRight.DELETE_TCUBE_USER.toString())) {
                MenuItem menu4 = new MenuItem();
                menu4.setValue(menuLocalization.getStaffMenuItem(AccessRight.DELETE_TCUBE_USER.toString()));
                menu4.setId(view.createUniqueId());
                String url11 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffDeleteAccount.xhtml";
                menu4.setUrl(url11);
                model.addMenuItem(menu4);
            } else if (staffAccessList.get(i).getAccessRight().toString().equalsIgnoreCase(AccessRight.EDIT_ACCESS_RIGHTS.toString())) {
                MenuItem menu5 = new MenuItem();
                menu5.setValue(menuLocalization.getStaffMenuItem(AccessRight.EDIT_ACCESS_RIGHTS.toString()));
                menu5.setId(view.createUniqueId());
                String url12 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffEditAccessRights.xhtml";
                menu5.setUrl(url12);
                model.addMenuItem(menu5);
            } else if (staffAccessList.get(i).getAccessRight().toString().equalsIgnoreCase(AccessRight.INACTIVATE_ACCOUNTS.toString())) {
                MenuItem menu6 = new MenuItem();
                menu6.setValue(menuLocalization.getStaffMenuItem(AccessRight.INACTIVATE_ACCOUNTS.toString()));
                menu6.setId(view.createUniqueId());
                String url12 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffInactivateAccounts.xhtml";
                menu6.setUrl(url12);
                model.addMenuItem(menu6);
            } else if (staffAccessList.get(i).getAccessRight().toString().equalsIgnoreCase(AccessRight.VERIFY_STAKEHOLDER_ACCOUNTS.toString())) {
                MenuItem menu7 = new MenuItem();
                menu7.setValue(menuLocalization.getStaffMenuItem(AccessRight.VERIFY_STAKEHOLDER_ACCOUNTS.toString()));
                menu7.setId(view.createUniqueId());
                String url13 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffVerifyStakeholderAcct.xhtml";
                menu7.setUrl(url13);
                model.addMenuItem(menu7);
            } else if (staffAccessList.get(i).getAccessRight().toString().equalsIgnoreCase(AccessRight.VIEW_ALL_STAFF.toString())) {
                MenuItem menu8 = new MenuItem();
                menu8.setValue(menuLocalization.getStaffMenuItem(AccessRight.VIEW_ALL_STAFF.toString()));
                menu8.setId(view.createUniqueId());
                String url14 = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffViewAll.xhtml";
                menu8.setUrl(url14);
                model.addMenuItem(menu8);
            }
        }
        return model;
    }

    //Create MailBox Menu
    private MenuModel generateMailBoxMenu() {
        String serverName = FacesContext.getCurrentInstance().getExternalContext().getRequestServerName();
        int serverPort = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("serverPort");
        String contextPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

        Submenu submenu = new Submenu();
        submenu.setId(view.createUniqueId());
        submenu.setLabel(menuLocalization.getStakeholderMenuItem("MY_MAIL_BOX"));

        MenuItem menu10 = new MenuItem();
        menu10.setId(view.createUniqueId());
        menu10.setValue(menuLocalization.getStakeholderMenuItem("COMPOSE_MAIL"));
        String URL = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffComposeMessage.xhtml";
        menu10.setUrl(URL);
        submenu.getChildren().add(menu10);
        model.addSubmenu(submenu);

        MenuItem menu11 = new MenuItem();
        menu11.setId(view.createUniqueId());
        menu11.setValue(menuLocalization.getStakeholderMenuItem("INBOX"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffMyInbox.xhtml";
        menu11.setUrl(URL);
        submenu.getChildren().add(menu11);
        model.addSubmenu(submenu);

        MenuItem menu12 = new MenuItem();
        menu12.setId(view.createUniqueId());
        menu12.setValue(menuLocalization.getStakeholderMenuItem("OUTBOX"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffMyOutbox.xhtml";
        menu12.setUrl(URL);
        submenu.getChildren().add(menu12);
        model.addSubmenu(submenu);

        MenuItem menu13 = new MenuItem();
        menu13.setId(view.createUniqueId());
        menu13.setValue(menuLocalization.getStakeholderMenuItem("TRASH"));
        URL = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffViewTrash.xhtml";

        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().containsKey("staffAccessRights")) {
            List<UserAccessRightsEntity> accessRights = staffAccountState.getAccessRights();
            for (int i = 0; i < accessRights.size(); i++) {
                if (accessRights.get(i).getAccessRight().toString().equals(AccessRight.MANAGE_MAILBOX.toString())) {
                    URL = "http://" + serverName + ":" + serverPort + contextPath + "/staff/StaffDeleteTrash.xhtml";
                }
            }
        }

        menu13.setUrl(URL);
        submenu.getChildren().add(menu13);
        model.addSubmenu(submenu);

        return model;
    }
}
